import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { addLeave, updateLeave } from "@/api/flandre-ui/request";
import { message } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { store } from "@/redux";
const LeaveDrawer = (props: { register: Fn; reload: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "HRMS.leaveApproval";
	const [action, setAction] = useState(""); // 用来区分新增还是修改
	const [currentLeaveId, setCurrentLeaveId] = useState("");

	const leaveFormSchema: FormSchema[] = [
		{
			label: t("felix_ui.staff_id"),
			field: "staffId",
			component: "Input",
			componentProps: {
				disabled: true
			},
			colProps: { span: 24 }
		},
		{
			label: t(`${en_vi}.staffName`),
			field: "staffName",
			component: "Input",
			componentProps: {
				disabled: true
			},
			colProps: { span: 24 }
		},
		{
			label: t(`${en_vi}.startDate`),
			field: "startDate",
			component: "DatePicker",
			colProps: { span: 24 },
			componentProps: { format: "YYYY-MM-DD" }
		},
		{
			label: t(`${en_vi}.endDate`),
			field: "endDate",
			component: "DatePicker",
			colProps: { span: 24 },
			componentProps: { format: "YYYY-MM-DD" }
		},
		{
			label: t(`${en_vi}.leaveType`),
			field: "leaveType",
			component: "Select",
			colProps: { span: 24 },
			componentProps: {
				options: [
					{ label: "Leave", value: "1" },
					{ label: "Overtime", value: "2" },
					{ label: "Compensatory Leave", value: "3" },
					{ label: "Business Trip", value: "4" },
					{ label: "Outing", value: "5" }
				]
			}
		},
		{
			label: t(`${en_vi}.reason`),
			field: "leaveReason",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("kevin_ui.status"),
			field: "status",
			component: "Select",
			colProps: { span: 24 },
			componentProps: {
				options: [
					{ label: "Regular", value: "0" },
					{ label: "Probative", value: "1" },
					{ label: "Resigned", value: "2" }
				]
			}
		}
	];

	const [registerForm, { validateFields, resetFields, setFieldsValue }] = useForm({
		labelWidth: 120,
		schemas: leaveFormSchema,
		showActionButtonGroup: false
	});

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		const user = store.getState().user.userInfo.user;
		resetFields();
		setFieldsValue({ staffName: user.nickname, staffId: user.id });
		const record = data.record || {};
		setAction(data.action || "add");
		if (data.action === "edit") {
			setCurrentLeaveId(record.leaveId);
			setFieldsValue(record);
		}
	});

	async function handleSubmit() {
		try {
			const values = await validateFields();
			if (values.startDate) values.startDate = values.startDate.split(" ")[0];
			if (values.endDate) values.endDate = values.endDate.split(" ")[0];
			if (action === "edit") {
				values.leaveId = currentLeaveId;
				await updateLeave(values);
				message.success(t("kevin_ui.updateSuccess"));
			} else {
				await addLeave(values);
				message.success(t("kevin_ui.addedSuccessfully"));
			}
			props.reload();
			closeDrawer();
		} catch (error) {
			message.error(t("kevin_ui.operationFailed"));
		}
	}

	return (
		<BasicDrawer
			{...props}
			title={action === "edit" ? t(`${en_vi}.editLeave`) : t(`${en_vi}.addLeave`)}
			register={registerDrawer}
			ok={handleSubmit}
		>
			<BasicForm register={registerForm} />
		</BasicDrawer>
	);
};

export default LeaveDrawer;
